//
//  MainTabBarController
//  HMWeibo07
//
//  Created by Xwoder on 15/7/27.
//  Copyright © 2015年 Xwoder. All rights reserved.
//

import UIKit
import ObjectiveC

class MainTabBarController : UITabBarController, MainTabBarDelegate {

    
    override func viewDidLoad() {
        super.viewDidLoad()

        // 添加所有的子视图控制器
        self.addChildViewControllers()

        // 当以下语句执行时，会调用MainViewController的viewDidLoad方法
        // window?.rootViewController = MainViewController()
        // 此时tabBar里没有items，即UIBarButtonItem
        // print(self.tabBar.items)
        
        // 利用KVC替换tabBar
        let mainTabBar = MainTabBar()
        self.setValue(mainTabBar, forKey: "tabBar")
        
        mainTabBar.compoaseButtonDelegate = self

    }
    
    func mainTabBarDidClickComposeButton(tabBar: MainTabBar) {
        print(__FUNCTION__)
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        // 当以下语句执行时，会调用MainViewController的viewWillAppear方法
        // window?.makeKeyAndVisible()
        // 此时items里数组里有东西了
        // print(self.tabBar.items)
    }
    
    // 添加所有的子视图控制器
    private func addChildViewControllers() {
        // 从JSON文件中加载加载控制器配置数据
        let mainViewControllerSettingJson: String! = NSBundle.mainBundle().pathForResource("MainTabBarControllerSettings", ofType: "json")
        let mainViewControllerSettingData: NSData! = NSData(contentsOfFile: mainViewControllerSettingJson)

        do {
            // 尝试从JSON文件中加载访问控制器配置信息
            let mainViewControllerSettings = try NSJSONSerialization.JSONObjectWithData(mainViewControllerSettingData, options: NSJSONReadingOptions(rawValue: 0)) as! [[String : String]]
            
            for setting in mainViewControllerSettings {
                addChildViewController(setting["viewControllerName"]!, title: setting["title"]!, imageName: setting["imageName"]!)
            }
        } catch {
            // 如果从JSON文件中加载加载控制器配置数据失败，则使用硬编码直接加载控制器
            // 首页
            self.addChildViewController("HomeTableViewController", title: "首页", imageName: "tabbar_home")
            // 消息
            self.addChildViewController("MessageTableViewController", title: "消息", imageName: "tabbar_message_center")
            // 发现
            self.addChildViewController("DiscoverTableViewController", title: "发现", imageName: "tabbar_discover")
            // 我
            self.addChildViewController("ProfileTableViewController", title: "我", imageName: "tabbar_profile")
        }
    }
    
    // 添加子控制器
    private func addChildViewController(viewControllerName: String, title: String, imageName: String) {
        
        // 从字符串获得类
        let bundleName = NSBundle.mainBundle().infoDictionary!["CFBundleName"] as! String
        let className = bundleName + "." + viewControllerName
        let viewControllerClass: AnyClass = NSClassFromString(className)!

        // 创建控制器实例
        let viewController: UIViewController = viewControllerClass.new() as! UIViewController
        
        NSObject.swiftClassFromString(viewControllerName)
        // 设置标题
        viewController.title = title
        
        // 使用UINavigationController封装一个控制器
        let navigationController = UINavigationController(rootViewController: viewController)
        
        // 设置图片
        navigationController.tabBarItem.image = UIImage(named: imageName)//!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        // 设置选中状态的图片
        navigationController.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")//!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        // 将控制器添加至tabBarController
        self.addChildViewController(navigationController)
    }
    

}

extension NSObject {
    class func swiftClassFromString(className: String) -> AnyClass? {
        if  let bundleName = NSBundle.mainBundle().infoDictionary!["CFBundleName"] as! String? {
            let className = bundleName + "." + className
            return NSClassFromString(className)
        } else {
            return nil
        }
    }
}
